﻿using GameSetting.Global;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GameSetting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetSettings();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if(radioBtnSword2.Checked) Globals.GameSetting.Type = (int)GameType.Sword2;
            if (radioBtnSworda.Checked) Globals.GameSetting.Type = (int)GameType.Sworda;
            if (radioBtnNewSword.Checked) Globals.GameSetting.Type = (int)GameType.NewSword;
            if (radioBtn640.Checked)
            {
                Globals.GameSetting.Width = 640;
                Globals.GameSetting.Height = 480;
            }
            if (radioBtn1280.Checked)
            {
                Globals.GameSetting.Width = 1280;
                Globals.GameSetting.Height = 960;
            }
            Globals.GameSetting.FullScreen = checkBoxFullScreen.Checked ? 1 : 0;
            Globals.GameSetting.IsUseThewWhenNormalRun = checkBoxIsUseThewWhenNormalRun.Checked ? 1 : 0;
            Globals.GameSetting.DebugMode = checkBoxDebugMode.Checked ? 1 : 0;
            Globals.GameSetting.SoundEffectVolume = (int)SoundSetting.Value;
            Globals.GameSetting.MusicVolume = (int)MusicSetting.Value;
            Globals.GameSetting.RunSpeedFold = (int)RunSpeedSetting.Value;

            Globals.SaveSetting();
        }
        
        private void btnRunGame_Click(object sender, EventArgs e)
        {
            Globals.SaveSetting();

            var gamepath = Globals.GetGamepath(); ;
            if (File.Exists(gamepath)){
                ProgramExecutor.ExecuteProgram(gamepath);
                Environment.Exit(0); // 立即退出主程序
            }
            else
            {
                MessageBox.Show($"游戏文件不存在，请确认游戏文件路径[{gamepath}]是否正确");
            }
        }
        private void SetSettings()
        {
            Globals.LoadSetting();
            switch ((GameType)Globals.GameSetting.Type)
            {
                case GameType.Sword2:
                    radioBtnSword2.Checked = true;
                    break;
                case GameType.Sworda:
                    radioBtnSworda.Checked = true;
                    break;
                case GameType.NewSword:
                    radioBtnNewSword.Checked = true;
                    break;
                default:
                    radioBtnSword2.Checked = true;
                    break;
            }
            if (Globals.GameSetting.Width >= 1280)
            {
                radioBtn1280.Checked = true;
            }
            else
            {
                radioBtn640.Checked = true;
            }
            checkBoxFullScreen.Checked = Globals.GameSetting.FullScreen > 0;
            checkBoxIsUseThewWhenNormalRun.Checked = Globals.GameSetting.IsUseThewWhenNormalRun > 0;
            checkBoxDebugMode.Checked = Globals.GameSetting.DebugMode > 0;
            SoundSetting.Value = Globals.GameSetting.SoundEffectVolume;
            MusicSetting.Value = Globals.GameSetting.MusicVolume;
            RunSpeedSetting.Value = Globals.GameSetting.RunSpeedFold;
        }
    }
}
